package com.zb.hms.data.base.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.zb.hms.data.base.dao.inter.BaseDaoInter;

@Repository
@SuppressWarnings(value={"rawtypes","deprecation", "unchecked" })
public class BaseDaoImpl<T> implements BaseDaoInter<T> {
	@Resource
	private SessionFactory factory;

	@Override
	public void save(T t) {
		// TODO Auto-generated method stub
		factory.getCurrentSession().save(t);
	}

	@Override
	public void saveList(List<T> ts) {
		// TODO Auto-generated method stub
		Session session=	factory.getCurrentSession();
		for(T t:ts){
			session.save(t);
		}
	}

	@Override
	public void delete(T t) {
		// TODO Auto-generated method stub
		factory.getCurrentSession().delete(t);
	}

	@Override
	public void deleteList(List<T> ts) {
		// TODO Auto-generated method stub
		Session session=	factory.getCurrentSession();
		for(T t:ts){
			session.delete(t);
		}
	}

	@Override
	public void update(T t) {
		// TODO Auto-generated method stub		
		factory.getCurrentSession().update(t);		
	}

	@Override
	public void updateList(List<T> ts) {
		// TODO Auto-generated method stub
		Session session=	factory.getCurrentSession();
		for(T t:ts){
			session.update(t);			
		}
	}

	@Override
	public List findHql(String Hql, Map<String,Object> params) {
		// TODO Auto-generated method stub
		Query	query=factory.getCurrentSession().createQuery(Hql);
		for(String param:params.keySet()){
			query.setParameter(param, params.get(param));
		}
		return query.list();											
	}

	@Override
	public T findId(Class<T> clazz, Serializable id) {
		// TODO Auto-generated method stub
		return factory.getCurrentSession().get(clazz, id);
	}
	@Override
	public List<T> findCriteria(Class<T> clazz, List<Criterion> criterions,
			List<Order> orders) {
		// TODO Auto-generated method stub
		Criteria criteria=	factory.getCurrentSession().createCriteria(clazz);
		for(Criterion criterion:criterions)
					criteria.add(criterion);
		for(Order order:orders)
			criteria.addOrder(order);		
		return  criteria.list();
	}
	@Override
	public List<T> findSql(String sql,Map<String,Object> params){
		// TODO Auto-generated method stub
		NativeQuery<T> query=  factory.getCurrentSession().createSQLQuery(sql);
		for(String param:params.keySet())
			query.setParameter(param, params.get(param));
		return query.list();		
	}

			
}
